from flask import Flask, request
from flask_sqlalchemy import SQLAlchemy

app = Flask(__name__)
app.config['SQLALCHEMY_DATABASE_URI'] = 'mysql://root:yuan199700..@42.193.42.76:3306/' \
                                        'zentao_helper'
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False

db = SQLAlchemy(app)


# class User(db.Model):
#     """用户密码表"""
#     __tablename__ = 't_user'
#     id = db.Column(db.Integer, primary_key=True)
#     user = db.Column(db.String(64), unique=True, index=True)
#     psd = db.Column(db.String(64))
#
#     def __repr__(self):
#         return self.user, self.pad


class Project(db.Model):
    """项目表"""
    __tablename__ = 't_project'
    id = db.Column(db.Integer, primary_key=True)
    name = db.Column(db.String(64), unique=True, index=True)
    url = db.Column(db.String(64))
    default = db.Column(db.Integer, default=0)

    def __repr__(self):
        return self.name, self.url, self.default


@app.route('/')
def index():
    return '数据库连接成功!'


@app.route('/project', methods=['POST'])
def project():
    """同步数据到本地"""
    data = request.json
    for k, v in data.items():
        db.session.add(Project(name=k, url=v[0],default=v[1]))
    db.session.commit()
    return '数据同步成功'


if __name__ == '__main__':
    db.drop_all()
    db.create_all()
    app.run()
